﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.RiegoSistemas;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.RiegoSistemas
{
	public partial class RiegoSistemasViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<RiegoSistema> _riegoSistemas;

		/// <summary>
		/// 
		/// </summary>
		private RiegoSistema _riegoSistema;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<RiegoSistema> RiegoSistemas
		{
			get
			{
				return _riegoSistemas;
			}
			private set
			{
				_riegoSistemas = value;
				if (_riegoSistemas != null)
					RaisePropertyChanged("RiegoSistemas");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public RiegoSistema RiegoSistema
		{
			get
			{
				return _riegoSistema;
			}
			private set
			{
				_riegoSistema = value;
				if (_riegoSistema != null)
					RaisePropertyChanged("RiegoSistema");
			}
		}

		#endregion

		/// <summary>
		/// 
		/// </summary>
		private void LoadRiegoSistemas()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IRiegoSistemaService service = IoCFactory.Resolve<IRiegoSistemaService>();
					e.Result = service.FindAllRiegoSistemas();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						RiegoSistemas = e.Result as List<RiegoSistema>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewRiegoSistema(object sender, EventArgs e)
		{
			_view.riegoSistemaBindingSource.AddingNew += AddingNewRiegoSistema;
			_view.riegoSistemaBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewRiegoSistema(object sender, AddingNewEventArgs e)
		{
			RiegoSistema = new RiegoSistema
			{
				Descripcion = string.Empty,
				Nombre = string.Empty,
				Orden = 0,
				SistemaGuid = Guid.NewGuid(),
				SistemaId = 0,
				Status = true,
			};
			RiegoSistema.MarkAsAdded();
			e.NewObject = RiegoSistema;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <param name="args"></param>
		private void SaveRiegoSistemas(object s, EventArgs args)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IRiegoSistemaService service = IoCFactory.Resolve<IRiegoSistemaService>();
					RiegoSistemas.ForEach(sistema =>
					{
						if (sistema.ChangeTracker.State == ObjectState.Added)
							service.AddRiegoSistema(sistema);
						else
							service.ChangeRiegoSistema(sistema);
						sistema.AcceptChanges();
					});
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						LoadRiegoSistemas();
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentRiegoSistema(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null && bindingSource.Current != null)
				RiegoSistema = bindingSource.Current as RiegoSistema;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnRiegoSistemasPropertyChanged()
		{
			_view.riegoSistemaBindingSource.DataSource = RiegoSistemas;
			_view.riegoSistemaBindingSource.CurrentChanged += SetCurrentRiegoSistema;
			SetCurrentRiegoSistema(_view.riegoSistemaBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnRiegoSistemaPropertyChanged()
		{

		}

		#endregion

	}
}
